/**
 * 校验手机号
 * @param val
 */
export const phone = (val) => {
  if (!val) {
    return
  }
  console.log(`val: ${val}, typeof: ${typeof val}`)
  val = val.replace(/\D/g, '')
  if (val.length > 15) {
    val = val.slice(0, 15)
  }
  return val
}

/**
 * 校验密码
 * @param val
 */
export const password = (val) => {
  if (!val) {
    return
  }
  // console.log(`val: ${val}, typeof: ${typeof val}`)
  val = val.replace(/[^a-zA-Z0-9]/g, '')
  // if (val.length > 20) {
  //   val = val.slice(0, 20)
  // }
  return val
}

/**
 * 校验卡号
 * @param val
 */
export const card = (val) => {
  if (!val) {
    return
  }
  // console.log(`val: ${val}, typeof: ${typeof val}`)
  val = val.replace(/\D/g, '')
  if (val.length > 20) {
    val = val.slice(0, 20)
  }
  return val
}


/**
 * 输入不为0开头的数字
 * @param val
 */
export const noZero = (val) => {
  if (!val) {
    return
  }
  val = (val.replace(/[^[0-9]/g, '').replace(/^0[0-9]*/g, ''))
  return val
}

/**
 * 只输入数字
 * @param val
 */
export const onlyNum = (val) => {
  if (!val) {
    return
  }
  val = (val.replace(/[^\d]/g, ''))
  return val
}

/**
 * 校验金额
 * @param val
 */
export const amount = (val) => {
  if (!val) {
    return
  }
  // console.log(`val: ${val}, typeof: ${typeof val}`)
  // 清除"数字"/"."以外的字符
  val = val.replace(/[^\d\.]/g, '')
  val = val.replace(/^\./g, "0."); //开头为小数点转换为0.
  // 只保留第一个.清除多余的
  val = val.replace(/\.{2,}/g, '.')
  // 只保留第一个-清除多余的
  // val = val.replace(/-{2,}/g, '-')
  val = val.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.')
  // 只能输入两个小数
  val = val.replace(/^(-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
  return val
}

/**
 * 金额相减法小数点、高精度小数问题
 */

export const preciseSubtract = (num1, num2, precision) => {
  let factor = Math.pow(10, precision ? precision : 2);
  num1 = Math.round(num1 * factor);
  num2 = Math.round(num2 * factor);

  return (num1 - num2) / factor;
}